clear
set more off
use "..\Data\transuniondata.dta", clear

/*****************************************************************FIGURES***************************************************************/

/******************************************
Figure 2: 
Open rate (see opengraph.dta)
View rate (see viewgraph.dta)
*******************************************/

/*******************************************
Figure 3: Treatment Effects by Quarter
********************************************/
set more off
foreach var in 30past6mo fico {
foreach qtr in S5 D5 M6 J6 S6 D6 M7 J7 {
mean `qtr'_`var' if T==0 
mat temp = e(b)
scalar ctrlmean = temp[1,1]

reg fd_`var'`qtr' T if Tm~=1, robust
estadd scalar mean = ctrlmean
estimates save "../estimates/itt_`var'_`qtr'.ster", replace
}
}

/******************************************
Appendix Figure 4: Treatment Effects by FICO Decile
*******************************************/
centile (J5_fico), centile (10 20 30 40 50 60 70 80 90)
gen ficod1=(J5_fico<=599)
gen ficod2=(J5_fico<=638 & J5_fico>599)
gen ficod3=(J5_fico<=655 & J5_fico>638)
gen ficod4=(J5_fico<=664 & J5_fico>655)
gen ficod5=(J5_fico<=675 & J5_fico>664)
gen ficod6=(J5_fico<=689 & J5_fico>675)
gen ficod7=(J5_fico<=703 & J5_fico>689)
gen ficod8=(J5_fico<=720 & J5_fico>703)
gen ficod9=(J5_fico<=743 & J5_fico>720)
gen ficod10=(J5_fico>743)
foreach i in 1 2 3 4 5 6 7 8 9 10 {
gen Txficod`i'=T*ficod`i'
}

reg fd_30past6moJ6 Txficod1 Txficod2 Txficod3 Txficod4 Txficod5 Txficod6 Txficod7 Txficod8 Txficod9 Txficod10 ficod1 ficod2 ficod3 ficod4 ficod5 ficod6 ficod7 ficod8 ficod9 if Tm~=1, robust
reg fd_ficoJ6 Txficod1 Txficod2 Txficod3 Txficod4 Txficod5 Txficod6 Txficod7 Txficod8 Txficod9 Txficod10 ficod1 ficod2 ficod3 ficod4 ficod5 ficod6 ficod7 ficod8 ficod9 if Tm~=1, robust



/*************************************************
Appendix Figure 5: View rate main vs disc (see viewgraph.dta)
**************************************************/

/************************************************************
Appendix Figure 6: View rate by treatment message (see viewgraph.dta)
************************************************************/

/*****************************************************************TABLES***************************************************************/

/********************************************************
Table 1: first stage
*********************************************************/
set more off
foreach var in viewby numviewby {
foreach qtr in J6 J7 {
mean `qtr'_`var' if T==0
mat temp = e(b)
scalar ctrlmean = temp[1,1]

reg `qtr'_`var' T if Tm~=1, robust
estadd scalar mean = ctrlmean
estimates save "../estimates/itt_`var'_`qtr'.ster", replace
}
}

/********************************************************
Table 2: main results
*********************************************************/
set more off
foreach var in 30past6mo fico fico620 {

mean J6_`var' if T==0
mat temp = e(b)
scalar ctrlmean = temp[1,1]
//ITT 
reg fd_`var'J6 T if Tm~=1, robust
estadd scalar mean = ctrlmean
estimates save "../estimates/itt_`var'_J6.ster", replacef
//IV open
ivregress 2sls fd_`var'J6 (J6_openby=Tj) if Tm~=1, first vce(robust)
estadd scalar mean = ctrlmean
estimates save "../estimates/ivopen_`var'_J6.ster", replace
//IV view
ivregress 2sls fd_`var'J6 (J6_viewby=Tj) if Tm~=1, first vce(robust)
estadd scalar mean = ctrlmean
estimates save "../estimates/ivview_`var'_J6.ster", replace
}

/*****************************
Table 3 (see survey do file)
******************************/

/**********************************************************
Table 4: late payment by delinquency/actionable reason code
(see reason code documentation for list of codes)
************************************************************/

foreach reason in 1 2 {
gen noaction`reason'=(inlist(J5_action`reason',"04","07","12","14","15","16","17","20") | inlist(J5_action`reason',"30"))
gen del`reason'=(inlist(J5_action`reason',"02","13","18","27","38","39","40"))
}
gen action=(noaction1==0 | noaction2==0)
gen anydel=(del1==1 | del2==1)
label var action "=1 if has an actionable baseline reason code"
label var anydel "=1 if has at least one delinquency-related baseline reason code"

gen Txaction=T*action
gen Txanydel=T*anydel

reg fd_30past6moJ6 Txanydel T anydel if Tm~=1, robust 
estimates save "../estimates/delreason_30past6mo_J6.ster", replace
reg fd_30past6moJ6 Txaction T action if Tm~=1, robust 
estimates save "../estimates/actreason_30past6mo_J6.ster", replace

/****************************************************************
Table 5: views and financial outcomes by main/discontinued
*****************************************************************/

set more off
foreach var in viewby {
foreach qtr in M6 M7 {
mean `qtr'_`var' if T==0
mat temp = e(b)
scalar ctrlmean = temp[1,1]

reg `qtr'_`var' Tj Tm, robust
test Tj=Tm
estadd scalar pval = r(p)
estadd scalar mean = ctrlmean
estimates save "../estimates/ittmar_`var'_`qtr'.ster", replace
}
}

set more off
foreach var in 30past6mo fico {
mean M7_`var' if T==0
mat temp = e(b)
scalar ctrlmean = temp[1,1]

reg fd_`var'M7 Tj Tm, robust
test Tj=Tm
estadd scalar pval = r(p)
estadd scalar mean = ctrlmean
estimates save "../estimates/ittmar_`var'_M7.ster", replace
}

/*************************************************APPENDIX***********************************************/

/****************************************
Appendix Table 1: summary statistics
*****************************************/

sum age inschool J5_prsn_on_file_mo_cnt J5_30past6mo J5_60past6mo J5_90past6mo J5_anyrev J5_numrev J5_revlimit J5_fico if T==0 //control
sum age inschool J5_prsn_on_file_mo_cnt J5_30past6mo J5_60past6mo J5_90past6mo J5_anyrev J5_numrev J5_revlimit J5_fico if Tm==0 & T==1 //main treat
sum age inschool J5_prsn_on_file_mo_cnt J5_30past6mo J5_60past6mo J5_90past6mo J5_anyrev J5_numrev J5_revlimit if Tm==1 //discontinued treat

foreach var in age inschool J5_prsn_on_file_mo_cnt J5_30past6mo J5_60past6mo J5_90past6mo J5_anyrev J5_numrev J5_revlimit J5_fico {
reg `var' T if Tm==0
}


/********************************************************
Appendix Table 2: (see survey do file)
*********************************************************/

/********************************************************
Appendix Table 3: views by quarter
*********************************************************/
set more off
foreach var in viewby numviewby {
foreach qtr in S5 D5 M6 J6 S6 D6 M7 J7 {
mean `qtr'_`var' if T==0
mat temp = e(b)
scalar ctrlmean = temp[1,1]

reg `qtr'_`var' T if Tm~=1, robust
estadd scalar mean = ctrlmean
estimates save "../estimates/itt_`var'_`qtr'.ster", replace
}
}

/********************************************************
Appendix Table 4
*********************************************************/
set more off
foreach var in anyrev numrev revlimit revbalamt {

mean J6_`var' if T==0
mat temp = e(b)
scalar ctrlmean = temp[1,1]
//ITT 
reg fd_`var'J6 T if Tm~=1, robust
estadd scalar mean = ctrlmean
estimates save "../estimates/itt_`var'_J6.ster", replacef

//IV open
ivregress 2sls fd_`var'J6 (J6_openby=Tj) if Tm~=1, first vce(robust)
estadd scalar mean = ctrlmean
estimates save "../estimates/ivopen_`var'_J6.ster", replace

//IV view
ivregress 2sls fd_`var'J6 (J6_viewby=Tj) if Tm~=1, first vce(robust)
estadd scalar mean = ctrlmean
estimates save "../estimates/ivview_`var'_J6.ster", replace
}

//multiple hypothesis test
*mhtexp fd_30past6moJ6 fd_anyrevJ6 fd_numrevJ6 fd_revbalamtJ6 fd_revlimitJ6 fd_ficoJ6 fd_fico620J6, treatment(T) //multiple hypothesis test


/*******************************************
Appendix Table 5: subgroup by FICO and Age
**********************************************/

set more off
foreach var in 30past6mo fico {

mean J6_`var' if T==0
mat temp = e(b)
scalar ctrlmean = temp[1,1]

reg fd_`var'J6 Txlowage T lowage if Tm~=1, robust
estadd scalar mean = ctrlmean
estimates save "../estimates/itt_subage_`var'_J6.ster", replace

reg fd_`var'J6 Txinschool T inschool if Tm~=1, robust
estadd scalar mean = ctrlmean
estimates save "../estimates/itt_substudent_`var'_J6.ster", replace
}

/******************************************************
Appendix Table 6: treatment effects by viewing timing
*******************************************************/

mean J6_30past12mo if T==0
mean J7_30past12mo if T==0

gen firstview_yr1=J6_viewby
gen firstview_yr2=(J6_viewby==0 & J7_viewby==1)

reg fd_30past12moJ6 T if Tm~=1 & firstview_yr2==0, robust 
reg fd_30past12moJ7 T if Tm~=1 & firstview_yr2==0, robust 

reg fd_30past12moJ6 T if Tm~=1 & firstview_yr1==0, robust 
reg fd_30past12moJ7 T if Tm~=1 & firstview_yr1==0, robust 

/*******************************************
Appendix Table 7: treatment effects by message type
**********************************************/
foreach var in 30past6mo fico {
mean J6_`var' if T==0
mat temp = e(b)
scalar ctrlmean = temp[1,1]

reg fd_`var'J6 T1 T2 T3 if Tm==0, robust
test T1=T2=T3
estadd scalar pval = r(p)
estadd scalar mean = ctrlmean
estimates save "../estimates/ittbyT_`var'_J6.ster", replace
}

/******************************************************
Appendix Table 8: (see survey do file)
*******************************************************/

/***************************************
Appendix Table B.1 (see survey do file)
****************************************/
